package org.snail.attendence.controller;

import org.apache.log4j.Logger;
import org.snail.attendence.service.impl.AttendenceMongoServiceImpl;
import org.snail.constant.ErrCodeConstant;
import org.snail.exception.CommonException;
import org.snail.exception.EmpException;
import org.snail.exception.JsonResult;
import org.snail.attendence.bean.AbnormalAttendanceDTO;
import org.snail.base.AttendenceCondition;
import org.snail.attendence.bean.AliWorkDTO;
import org.snail.attendence.bean.AttendenceMessage;
import org.snail.attendence.mapper.AttendenceMongoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/attendenceMongo")
@Controller
public class AttendenceMongoController {
    private static final Logger LOGGER =  Logger.getLogger(AttendenceMongoController.class);
    @Autowired
    private AttendenceMongoServiceImpl attendenceMongoService;
    @Autowired
    private AttendenceMongoMapper attendenceMongoMapper;

    @RequestMapping("findAllAttendence")
    @ResponseBody
    public JsonResult findAllAttendence(){
        JsonResult jsonResult = new JsonResult();
        List<AliWorkDTO> aliWorkDTOs = attendenceMongoService.findAll();
        jsonResult.setData(aliWorkDTOs);
        jsonResult.setMessage("考勤信息查询成功");
        return jsonResult;
    }
    /**
     * 根据条件查询考勤列表
     *
     * @param aliCondition
     * @return result 里面封装了AbAttendenceMomgo
     * @throws org.snail.exception.EmpException
     */
    @RequestMapping("findAliListByCondition")
    @ResponseBody
    public JsonResult findAliListByCondition(AttendenceCondition aliCondition) throws CommonException, EmpException {
        JsonResult result = new JsonResult();
        List<AbnormalAttendanceDTO> aliListByMix =  attendenceMongoService.findAbnornalList(aliCondition);
        result.setCode(ErrCodeConstant.SUCCESS);
        result.setData(aliListByMix);
        return result;
    }

    /**
     * 标记为异常
     * @param abnormalAttendanceDTO
     * @return  jsonResult
     */
    @ResponseBody
    @RequestMapping("/editNormal")
    public JsonResult editNormal(AbnormalAttendanceDTO abnormalAttendanceDTO) throws CommonException {
        if (null == abnormalAttendanceDTO){
            LOGGER.error("异常考勤对象为空！");
            throw new CommonException(ErrCodeConstant.ILLEGAL_PARAM, "异常对象为空！");
        }
        attendenceMongoService.editNormal(abnormalAttendanceDTO);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(ErrCodeConstant.SUCCESS);
        jsonResult.setMessage("标记成功");
        return jsonResult;
    }

    @RequestMapping("saveAttendaceMessage")
    @ResponseBody
    public JsonResult saveAttendaceMessage(@RequestBody List<AttendenceMessage> attendenceMessageList) {
        JsonResult result = attendenceMongoService.saveAttendaceMessage(attendenceMessageList);
        return result;
    }


    /**
     * 添加或者是修改相关字段，保证添加时，不重复添加
     * @param aliWorkDTOS list<aliWork>
     * @return   msg,现在也没什么用
     */
    @ResponseBody
    @RequestMapping("/addOrUpDateAttendence")
    public StringBuilder addOrUpDateAttendence(List<AliWorkDTO> aliWorkDTOS) {
        StringBuilder msg = new StringBuilder();
        if (aliWorkDTOS.isEmpty()) {
            msg.append("列表为空");
        } else {
            attendenceMongoService.addOrUpDateAttendence(aliWorkDTOS);   //表示查询所有
            msg.append("success");
        }
        return msg;
    }



    @RequestMapping("findAttendaceMessage")
    @ResponseBody
    public JsonResult findAttendaceMessage() {
        JsonResult result = attendenceMongoService.findAttendaceMessage();
        return result;
    }


    /**
     * 根据isCorrect查询异常列表
     * @return jsonResult包含abnormalAttendanceDTOS   abnormalAttendandencesDTO的列表
     */
    @RequestMapping("findAbnornalListByAliCondition")
    @ResponseBody
    public JsonResult findAbnornalList(AttendenceCondition aliCondition) throws EmpException {
        JsonResult jsonResult = new JsonResult();
        List<AbnormalAttendanceDTO> abnormalAttendanceDTOS = attendenceMongoService.findAbnornalList(aliCondition);
        jsonResult.setData(abnormalAttendanceDTOS);
        jsonResult.setCode(ErrCodeConstant.SUCCESS);
        return jsonResult;
    }

    @RequestMapping("test")
    @ResponseBody
    public JsonResult test(AttendenceCondition aliCondition) throws EmpException {
        JsonResult jsonResult = new JsonResult();
        List<AbnormalAttendanceDTO> abnormalAttendanceDTOS = attendenceMongoMapper.getFastInquiryList(aliCondition.getFastInquiry());
        jsonResult.setData(abnormalAttendanceDTOS);
        jsonResult.setCode(ErrCodeConstant.SUCCESS);
        return jsonResult;
    }


    }
